package de.unidue.langtech.teaching.pp.example;

import java.util.Collection;

import org.apache.uima.UimaContext;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.fit.component.JCasAnnotator_ImplBase;
import org.apache.uima.fit.util.JCasUtil;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import de.unidue.langtech.teaching.pp.type.TimexEvent;

/**
 * Annotator to check timex events for correct value.
 * 
 * @author Inui
 */
public class TimeEvaluator extends JCasAnnotator_ImplBase {

	private int correct;
	private int counter;

	/**
	 * Initializes the annotator with the given UIMA context.
	 */
	@Override
	public void initialize(UimaContext context) throws ResourceInitializationException {
		super.initialize(context);
		correct = 0;
		counter = 0;
	}

	/**
	 * Processes the given JCas and checks for each timex event contained
	 * therein, if the detected value and the gold value are equal.
	 * 
	 * @param jcas the JCas
	 * @throws AnalysisEngineProcessException if anything goes wrong.
	 */
	@Override
	public void process(JCas jcas) throws AnalysisEngineProcessException {
		Collection<TimexEvent> timexEvents = JCasUtil.select(jcas,
				TimexEvent.class);
		for (TimexEvent timexEvent : timexEvents) {
			System.out.println("GoldTime: " + timexEvent.getGoldValue()
					+ " -> DetectedTime: " + timexEvent.getDetectedValue()
					+ " - (identified natural language: " + timexEvent.getTimexText() + ")");
			counter++;
			if (timexEvent.getDetectedValue().equals(timexEvent.getGoldValue())) {
				correct++;
			}
		}
	}

	/**
	 * Prints out figures concerning correctly determined values.
	 * 
	 * @throws AnalysisEngineProcessException
	 *             if anyting goes wrong.
	 */
	@Override
	public void collectionProcessComplete() throws AnalysisEngineProcessException {
		super.collectionProcessComplete();
		System.out.println("======================================");
		System.out.println("Result: " + counter + " timex events were found.");
		System.out.println(correct + "/" + counter + " (" + correct * 100 / counter + " %) " + "are correct.");
		System.out.println("======================================");
	}
}